/**
 * 
 */
package com.jiang.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * @author Administrator
 * @登录拦截器
 */
@SuppressWarnings("serial")
public class LoginInterceptor extends AbstractInterceptor {

	private String loginKey; // 用户登录后存的key

	public String getLoginKey() {
		return loginKey;
	}

	public void setLoginKey(String loginKey) {
		this.loginKey = loginKey;
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object loginData = session.getAttribute(loginKey);
		String page = null;
		if(loginData != null) { // 说明登录过了，可以继续
			page = invocation.invoke();
			System.out.println("登录成功!");
		}else{
			request.setAttribute("msg","您还未登录,请登入先!");
			// 说明还未登录
			page = "forward.page";
		}
		return page;
	}

}
